package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.enums.*;
import com.example.generator.domain.Order;
import com.example.generator.domain.Salesman;
import com.example.generator.domain.TWork;
import com.example.generator.service.OrderService;
import com.example.generator.service.SalesmanService;
import com.example.generator.service.TWorkService;
import com.example.vo.SalesmanOrderSumVo;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：chengyoubang
 * @version ：V1.0
 * @program ：first-service
 * @date ：Created in 2023/12/23 12:37
 * @description ：haha
 */
@RestController
public class HappyWorkController {

    @Resource
    TWorkService workService;

    @Resource
    OrderService orderService;

    @Resource
    SalesmanService salesmanService;



    @PostMapping("haha")
    public String happyWork(@RequestBody TWork work) {
        work.setAddTime(new Date());
        Double score = getScore(work);//得分
        work.setScore(score);
        workService.save(work);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("score", score);
        jsonObject.put("level", getLevel(score));
        return jsonObject.toString();
    }

    @GetMapping("test")
    public String test() {
        Order byId = orderService.getById(1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("order", byId);
        Salesman byId1 = salesmanService.getById(1);
        jsonObject.put("salesman", byId1);
        return jsonObject.toString();
    }

    @GetMapping("getOrderByDate")
    public String getOrderByDate(@RequestParam(required = false) String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            dateStr = DateUtil.format(new Date(), "yyyy-MM-dd");
        }
        List<Order> list = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getOrderDate, dateStr).orderByAsc(Order::getSalesmanId));
        JSONObject jsonObject = new JSONObject();
        if (CollectionUtils.isEmpty(list)) {
            List<Salesman> salesmanList = salesmanService.list(new LambdaQueryWrapper<Salesman>().orderByAsc(Salesman::getSortId));
            list = new ArrayList<>();
            for (Salesman salesman : salesmanList) {
                Order order = new Order();
                order.setSalesmanId(salesman.getSalesmanId());
                order.setSalesmanName(salesman.getSalesmanName());
                order.setAddTime(new Date());
                order.setUpdateTime(new Date());
                order.setOrderDate(dateStr);
                order.setSortId(salesman.getSortId());
                list.add(order);
            }
            orderService.saveBatch(list);
        }
        List<Order> collect = list.stream().sorted(Comparator.comparing(Order::getSortId)).collect(Collectors.toList());
        jsonObject.put("orders", collect);
        jsonObject.put("dateStr", dateStr);
        return jsonObject.toString();
    }

    @PostMapping(value = "calculateOrder")
    public JSONObject calculate(@RequestBody List<Object> orders) {
        List<Order> orders2 = BeanUtil.copyToList(orders, Order.class);
        List<Order> newOrder = orders2.stream().filter(o -> o.getOrderId() != null).collect(Collectors.toList());
        for (Order order : newOrder) {
            order.calculate();
        }
        orderService.updateBatchById(newOrder);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orders", newOrder);
        return jsonObject;
    }

    @GetMapping(value = "statisticsOrderByDate")
    public JSONObject statisticsOrder(@RequestParam String startDate, @RequestParam String endDate) {
        List<SalesmanOrderSumVo> orders = orderService.sumOrderByDate(startDate, endDate);
        JSONObject object = new JSONObject();
        object.put("orders",orders);
        return object;
    }




    //工作性价比=平均日薪×综合环境系数/35×(工作时长＋通勤时长-0.5×摸鱼时长)×学历系数
    //综合环境系数=工作环境系数×异性环境系数×同事环境系数
    private Double getScore(TWork tWork) {
        Double workDay = RestDayEnum.getTrueDay(tWork.getRestDay());
        int daySalary = (int) Math.round(tWork.getSalary() / workDay);
        tWork.setDaySalary(daySalary);
        //学历系数
        Double eb = EducationalBackGroundEnum.getRatio(tWork.getEducationalBackground());
        //工作环境
        Double wr = WorkAreaEnum.getRatio(tWork.getWorkArea());
        ///异性
        Double os = OppositeSexEnum.getRatio(tWork.getOppositeSex());
        wr *= os;
        //同事关系
        Double cr = ColleagueRelationshipEnum.getRatio(tWork.getColleagueRelationship());
        wr *= cr;
        //工作压力
        Double ws = WorkStressEnum.getRatio(tWork.getWorkStress());
        wr *= ws;
        //工作稳定性
        Double wst = WorkStableEnum.getRatio(tWork.getWorkStable());
        wr *= wst;
        //职业要求
        Double or = OccupationalRequirementEnum.getRatio(tWork.getOccupationalRequirement());
        wr *= or;
        Double fz = daySalary * wr;
        Double fm = 35 * eb * (tWork.getDayWorkTime() + tWork.getCommutingTime() - (0.5 * tWork.getMoyuTime()));
        Double score = fz / fm;
        BigDecimal bigDecimal = new BigDecimal(score);
        double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return v;
    }

    private String getLevel(Double score) {
        //这个指标给出的结论是：如果你工作性价比>2.0，就是神仙工作；>1.5的话工作还不错；而<0.8，
        if (score >= 1.9) {
            return "绝世好工作，大神带带我！！";
        } else if (score >= 1.4) {
            return "很棒哦，您已打败80%的打工人了！！";
        } else if (score >= 0.7) {
            return "很不错了哦，离好工作就差一丢丢";
        } else {
            return "要不考虑换一个吧";
        }
    }
}

